package com.sgb.kjwl.view.widget;

import android.content.Context;
import android.widget.TextView;
import android.widget.Toast;

import com.bigkoo.pickerview.adapter.ArrayWheelAdapter;
import com.contrarywind.adapter.WheelAdapter;
import com.contrarywind.listener.OnItemSelectedListener;
import com.contrarywind.view.WheelView;
import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.transport.PublicSourceEntity3;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.dialog.PopViewHolder;

import java.util.List;

public class GoodsInfoPublicSelectPopWindow3 extends BasePopWindow {

    private TextView titleView;
    private List<PublicSourceEntity3> items;
    private String titleName = "";
    private WheelView wheelView;
    private PublicSourceEntity3 entity;

    public GoodsInfoPublicSelectPopWindow3(Context c) {
        super(c,DIR_DOWN_UP);
    }

    @Override
    protected int popLayout() {
        return R.layout.pop_window_goods_info_public3;
    }

    @Override
    protected void initView(PopViewHolder holder) {
        TextView okView = holder.getPop().findViewById(R.id.ok_view);
        TextView resetView = holder.getPop().findViewById(R.id.reset_view);
        titleView = holder.getPop().findViewById(R.id.title);
        wheelView = holder.getPop().findViewById(R.id.wheelview);
        wheelView.setCyclic(false);

        titleView.setText(titleName);

        okView.setOnClickListener(view -> {
            onResultCallBack.result(entity);
            dissMiss();
        });

        resetView.setOnClickListener(view -> dissMiss());
    }

    public void setData(String title,List<PublicSourceEntity3> list){
        this.items = list;
        this.titleName = title;

        wheelView.setAdapter(new SelectAdapter(items));
        wheelView.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(int index) {
                entity = items.get(index);
            }
        });
    }

    public static class SelectAdapter implements WheelAdapter{

        private List<PublicSourceEntity3> items;

        SelectAdapter(List<PublicSourceEntity3> list){
            items = list;
        }

        @Override
        public int getItemsCount() {
            return items == null? 0 : items.size();
        }

        @Override
        public Object getItem(int index) {
            if (index >= 0 && index < items.size()) {
                return items.get(index).getName();
            }
            return "";
        }

        @Override
        public int indexOf(Object o) {
            return items.indexOf(o);
        }
    }
}
